$('#fields_choice').change(function(e) {
	/* Get all field's informations */
	var id_field = $(this).val();
	$.ajax({
		url: '<?php echo $this->baseUrl; ?>/formations/getfield/id/' + id_field + '/format/html',
		success: function(e) {
			if(e == 'ERROR_MISSING_ID') {
				/* Show error */
				var mess = '<div class="error">' + e + '</div>';
				$('#field_add_mess').html(mess);
				$('#field_add_mess').show();
				setTimeout("$('#field_add_mess').hide()", 3000);
			} else {
				$('#field_add_new').html(e);
				$('#field_add_new').show();
			}
		}
	});
});

$('.field_delete').live('click', function() {
	/* Hide & delete field */
	/* In field add */
	$('#field_' + $(this).attr('field_name') + '_div').fadeOut();
	$('#field_' + $(this).attr('field_name') + '_div').remove();
	
	/* In form */
	$('#' + $(this).attr('field_name') + '_form_div').fadeOut();
	$('#' + $(this).attr('field_name') + '_form_div').remove();
	
	/* Show in select list */
	$('#' + $(this).attr('field_name') + '_option').show();
});

$('.field_add').live('click', function() {
	
	/* Get value(s) */
	
	if($(this).attr('field_type') == 'text' || $(this).attr('field_type') == 'file') {
		var id = $('#' + $(this).attr('field_name')).attr('id');
		var value = $('#' + $(this).attr('field_name')).val();
		
	}
	else if($(this).attr('field_type') == 'checkbox') {
		var id = '';
		var value = '';
		$(':checkbox[name="' + $(this).attr('field_name') + '[]"]').each(function(e) {
			if($(this).attr('checked')) {
				id += $(this).attr('id') + ';';
				value += $(this).val() + ';';
			}
		});
		
	} else if($(this).attr('field_type') == 'radio') {
		var id = '';
		var value = '';
		$(':radio[name="' + $(this).attr('field_name') + '"]').each(function(e) {
			if($(this).attr('checked')) {
				id += $(this).attr('id') + ';';
				value += $(this).val() + ';';
			}
		});
	}
	
	/* Is modifiable ? */
	var modifiable = $('#' + $(this).attr('field_name') + '_modifiable').attr('checked');
	
	/* Add new field content to the good section */
	$('#' + $(this).attr('part') + '_div').append('<div id="' + $(this).attr('field_name') + '_form_div">' + $('#field_' + $(this).attr('field_name') + '_div').html() + '</div>');
	
	
	/* Hide & delete temp field */
	$('#field_' + $(this).attr('field_name') + '_div').fadeOut();
	$('#field_' + $(this).attr('field_name') + '_div').remove();
	
	/* Hide & delete Add button */
	$('#field_add_send').remove();
	
	/* Hide & delete Informations div if exist */
	$('#field_informations').remove();
	
	/* Hide in select list */
	$('#' + $(this).attr('field_name') + '_option').hide();
	
	/* Reset selection in select list */
	$('#default_option').attr('selected', 'selected');
	
	/* Add values */
	if($(this).attr('field_type') == 'text' || $(this).attr('field_type') == 'file') {
		$('#' + id).val(value);
		
	} else {
		var value_tab = value.split(';');
		var id_tab = id.split(';');
		var i = 0;
	
		for(i; i < (value_tab.length - 1); i ++) {
			$('#' + id_tab[i]).val(value_tab[i]);
			$('#' + id_tab[i]).attr('checked', 'checked');
		}
		
	}
	
	/*
	if($(this).attr('field_type') == 'text' || $(this).attr('field_type') == 'file') {
		$('#' + $(this).attr('field_name')).val(temp);
		
	} else if($(this).attr('field_type') == 'checkox') {
		
		
	}
	*/
	
	if(modifiable) {
		$('#' + $(this).attr('field_name') + '_modifiable').attr('checked', 'checked');
	}
});

$(document).ready(function(e) {
	$('#createsheet_cancel').click(function(e){

		/* Delete project's call */
		
		/* Go to project's call form */
		
	});
	
	$('#createsheet_send_add').click(function(e){
	
		/* Submit and show next form */
		$('#createsheet_form').validate({
			onkeyup: false,
			submitHandler: function(form) {
			
				$(form).ajaxSubmit({
					beforeSend: function(e) {
						
					},
					success: function(e) {
						
						if(e == 'ERROR_NO_PARAMS') {
							$('#createsheet_send_mess').html('<div class="error"><?php echo str_replace('\n', '', $this->translate('FORMATIONS ERROR PARAMS')); ?></div>');
							$('#createsheet_send_mess').fadeIn();
						} else if(e == '') {
							$('#createsheet_send_mess').html('<div class="information"><?php echo str_replace('\n', '', $this->translate('FORMATIONS SUCCESS CREATION')); ?></div>');
							$('#createsheet_send_mess').fadeIn();
							setTimeout('document.location.reload()', 3000);
						} else {
							$('#createsheet_send_mess').html('<div class="error"><?php echo str_replace('\n', '', $this->translate('FORMATIONS ERROR CREATION')); ?></div>');
							$('#createsheet_send_mess').fadeIn();
						}
						return false;
					}
				});
				return false;
			}
		});
	});
	
	$('#createsheet_send_end').click(function(e){
		/* Submit and go next step */
		$('#createsheet_form').validate({
			onkeyup: false,
			submitHandler: function(form) {
			
				$(form).ajaxSubmit({
					beforeSend: function(e) {
						
					},
					success: function(e) {
						
						if(e == 'ERROR_NO_PARAMS') {
							$('#createsheet_send_mess').html('<div class="error"><?php echo str_replace('\n', '', $this->translate('FORMATIONS ERROR PARAMS')); ?></div>');
							$('#createsheet_send_mess').fadeIn();
						} else if(e == '') {
							$('#createsheet_send_mess').html('<div class="information"><?php echo str_replace('\n', '', $this->translate('FORMATIONS SUCCESS CREATION')); ?></div>');
							$('#createsheet_send_mess').fadeIn();
							setTimeout('document.location.href=\'<?php echo $this->url(array('controller' => 'formations', 'action' => 'projectcallsheets'), null, true); ?>/pc/' + <?php echo $this->id_project_call; ?> + '\'', 3000);
						} else {
							$('#createsheet_send_mess').html('<div class="error"><?php str_replace('\n', '', $this->translate('FORMATIONS ERROR CREATION')); ?></div>');
							$('#createsheet_send_mess').fadeIn();
						}
						return false;
					}
				});
				return false;
			}
		});
	});
});
